import _ from 'lodash';

class TreeFormatNode {

    getTreeNode(nodes){
        const gc = function(parent){
            let cn = [] , temp;
            _.forEach(nodes,function(item: any){
                if(item.parent === parent){
                    let obj:any = {
                        label: item.name,
                        key: item.id,
                        value: item.id
                    }

                    temp = gc(item.id);

                    if(!!temp && temp.length>0){
                        obj.children = temp;
                        obj.isLeaf = false;
                    }else {
                        obj.isLeaf = true;
                    }

                    cn.push(obj);
                }
            })

            return cn;
        }

        return gc(0);
    }
}

export const treeFormatNode = new TreeFormatNode();